package com.graduation.springbootinit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduation.springbootinit.common.BaseResponse;
import com.graduation.springbootinit.common.ErrorCode;
import com.graduation.springbootinit.common.ResultUtils;
import com.graduation.springbootinit.exception.BusinessException;
import com.graduation.springbootinit.model.entity.CulturePart.CulturePartDiscussion;
import com.graduation.springbootinit.model.entity.User;
import com.graduation.springbootinit.service.CulturePartDiscussionService;
import com.graduation.springbootinit.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 文化讨论区接口
 */
@RestController
@RequestMapping("/api/culture/discussion")
public class CulturePartDiscussionController {

    @Resource
    private CulturePartDiscussionService culturePartDiscussionService;

    @Resource
    private UserService userService;

    /**
     * 创建讨论
     *
     * @param discussion 讨论信息
     * @param request HTTP请求
     * @return 讨论id
     */
    @PostMapping("/add")
    public BaseResponse<Long> addDiscussion(@RequestBody CulturePartDiscussion discussion, HttpServletRequest request) {
        if (discussion == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        long discussionId = culturePartDiscussionService.createDiscussion(discussion, loginUser.getId());
        return ResultUtils.success(discussionId);
    }

    /**
     * 删除讨论
     *
     * @param id 讨论id
     * @param request HTTP请求
     * @return 是否成功
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteDiscussion(@RequestParam long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean result = culturePartDiscussionService.deleteDiscussion(id, loginUser.getId());
        return ResultUtils.success(result);
    }

    /**
     * 更新讨论
     *
     * @param discussion 讨论信息
     * @param request HTTP请求
     * @return 是否成功
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateDiscussion(@RequestBody CulturePartDiscussion discussion, HttpServletRequest request) {
        if (discussion == null || discussion.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean result = culturePartDiscussionService.updateDiscussion(discussion, loginUser.getId());
        return ResultUtils.success(result);
    }

    /**
     * 根据id获取讨论
     *
     * @param id 讨论id
     * @return 讨论信息
     */
    @GetMapping("/get")
    public BaseResponse<CulturePartDiscussion> getDiscussionById(@RequestParam long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        culturePartDiscussionService.incrementViewCount(id);
        CulturePartDiscussion discussion = culturePartDiscussionService.getDiscussionById(id);
        return ResultUtils.success(discussion);
    }

    /**
     * 分页获取讨论列表
     *
     * @param current 当前页码
     * @param pageSize 页面大小
     * @return 分页结果
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<CulturePartDiscussion>> listDiscussionByPage(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long pageSize) {
        if (current <= 0 || pageSize <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<CulturePartDiscussion> queryWrapper = new QueryWrapper<>();
        Page<CulturePartDiscussion> page = culturePartDiscussionService.page(
                new Page<>(current, pageSize), queryWrapper);
        return ResultUtils.success(page);
    }

}